 app.controller('OrderPublishCtrl', function ($scope, $ionicModal, $ionicPlatform,
  											$cordovaFile ,$cordovaMedia,$ionicLoading,$cordovaCamera,
 											$timeout,Upload,UploadService,ConfigMap, RecordService, OrderService){
	 		
	 		$scope.photoList = [];


	 	 	var record = RecordService.record;
	 	 	$scope.record = record;

			$scope.takePhoto = function(){
				var photo = {
		 			localUrl:"",
		 			serverUrl:""
	 			};
	 			$scope.photoList.push(photo);
				var options = {
					destinationType: Camera.DestinationType.FILE_URI,
					sourceType: Camera.PictureSourceType.PHOTO
				};
				$cordovaCamera.getPicture(options).then(function(imageURI){	
					photo.localUrl = imageURI;
					UploadService.uploadImage(imageURI, function(data){
						photo.serverUrl = data.msg;
						$scope.$digest();
					}, function(err){
						alert("上传失败"+imageURI);
					});
				});
			};

			$scope.deletePhoto = function(photo){
				$scope.photoList.removevalue(photo);
			};

	 		$scope.startRecord = function() {
	 			RecordService.createNewMedia($scope);
	 			RecordService.startRecord();
	    	};

	    	$scope.playRecord = function(){
	    		RecordService.playRecord();
	    	};

	    	$scope.reRecord = function(){
	    		RecordService.reRecord();
	    	};

		    $scope.stopRecord = function(){
		    	RecordService.stopRecord();
		    };	
		
			$scope.publish = function(){
				alert(record.finalRadioSrc);
				if(!record.finalRadioSrc){
					alert('必须有录音');
					return;
				}
				var imageUrls = [];
				for(var i = 0,length = $scope.photoList.length;i<length;i++){
					var serverUrl = $scope.photoList[i].serverUrl;
					if(serverUrl){
						imageUrls.push(serverUrl);
					}else{
						$scope.photoList.splice(i,1);
						alert('有一张照片上传有点问题，请重新上传');
						return;
					}
					
				}
				
				OrderService.publish(record.finalRadioSrc, imageUrls, function(data){
					writeObj(data);
					if(data.code == 200){
						alert('发布成功');
					}else{
						alert(data.msg);
					}
				});
			};
		});
